package dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import pojo.Customer;
import util.DBUtil;

public class AddDao {
	public static int findId(String name){
		Connection con = null;
		String sql = "select id,name from customer";
		PreparedStatement pst = null;
		ResultSet rs = null;
		int id = 0;
		try {
			con = DBUtil.createConnection();
			pst=con.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()){
				if(name.equals(rs.getString("name"))){
				id = rs.getInt("id");
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con,pst,rs);
		}
		return id;
	}
	
	public static int addCustomer1(Customer customer) {
		// TODO Auto-generated method stub
		String sql = "insert into customer(id,name,sal,birth,sex) values (seq01.nextval,?,?,?,?)";
		Connection con =null;
		PreparedStatement pst = null;
		int m = 0;
		try {
			con = DBUtil.createConnection();
			pst=con.prepareStatement(sql);
			pst.setString(1, customer.getCname());
			pst.setDouble(2, customer.getSal());
			pst.setDate(3, new java.sql.Date(customer.getBirth().getTime()));
			pst.setBoolean(4, customer.getSex());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con,pst,null);
		}
		
		System.out.println("sdasdasdasd     "+  m);
		return m;
	}
	
	public static int addHobby(int id,String fav) {
		int m = 0;
		Connection con = null;
		String sql = "insert into hobby(id,name_id,fav) values (seq_hobby_01.nextval,?,?)";
		PreparedStatement pst = null;
		try {
			con = DBUtil.createConnection();
			pst=con.prepareStatement(sql);
			pst.setInt(1, id);
			pst.setString(2, fav);
			System.out.println(22222);
			m=pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con, pst, null);
		}
		return m;
	}
}
